import { $delete, $get, $post, $put, requestOneFile } from "@src/assets/js/request"
import { compressLicenseAndMobile, decompressLicenseAndMobile } from "@src/assets/js/tool"



// 更新或新增当事人数据
export async function agentRequest (p) {
  if (p.front instanceof File) {
    const res = await requestOneFile(p.front)
    p.fileid = res.id
  }
  if (p.back instanceof File) {
    const res = await requestOneFile(p.back)
    p.fileid1 = res.id
  }
  compressLicenseAndMobile(p)
  const id = p.id
  delete p.id
  delete p.front
  delete p.back
  delete p.file
  delete p.file1
  let res
  if (id === undefined) {
    res = await $post('/api/xd/agent', p) // 新增数据
  } else {
    res = await $put(`/api/xd/agent/${id}`, p) // 更新数据
  }
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}

// 获取代理人信息
export async function getRequest (id) {
  const res = await $get(`/api/xd/agent/${id}`)
  decompressLicenseAndMobile(res.data) 
  return res.data
}


// 删除代理人
export async function delRequest (id) {
  const res = await $delete(`/api/xd/agent/${id}`)
  if (res.status !== 1) {
    throw new Error(res.message)
  }
  return res.data
}
